import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:luckiness/tool_bar/order.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../utils/model/login_info.dart';
import '../utils/model/order_info.dart';
import 'package:toast/toast.dart';
import 'dart:convert';
/// 状态栏高度
double statusBarHeight(BuildContext context) {
  double db =  MediaQuery.of(context).padding.top > 20 ? MediaQuery.of(context).padding.top : 20 ;
  return db;
}

/// 底部tabbar高度
double bottomTabBarHeight(BuildContext context) {
  return statusBarHeight(context) > 21 ? 83 : 49;
}

/// 宽度
double screenWidth(BuildContext context) {
  return MediaQuery.of(context).size.width;
}

/// 宽度
double screenHeight(BuildContext context) {
  return MediaQuery.of(context).size.height;
}

/// 宽度系数
double widthScale(BuildContext context) {
  return MediaQuery.of(context).size.width / 375.0;
}

/// 分割线颜色
Color separatorLineColor = Color.fromRGBO(242, 242, 242, 1.0);

/// 显示提示信息
showMessage(BuildContext context, String mes){
  Toast.show(mes, context, duration: 3);
}


double ffSavePointNumber(double origin, int num) {
  if (origin.toString().length - origin.toString().lastIndexOf('.') - 1 < num) {
    print(origin.toStringAsFixed(num));
    return double.parse(origin.toStringAsFixed(num).substring(0, num.toString().lastIndexOf(".")+ num +1));
  }else {
    return double.parse(origin.toString().substring(0, origin.toString().lastIndexOf('.') + num + 1));
  }
}

class FFShareInstance {
  // static FFShareInstance _instance = FFShareInstance._internal();
  // FFShareInstance._internal();
  // factory FFShareInstance() {
  //   return _instance;
  // }

  static SharedPreferences prefs;

  static Future init() async {
    await FFShareInstance.getLoginInfo();
  }

  static LoginInfo info = LoginInfo();
  static OrderInfo order;
  static getLoginInfo() async {
    WidgetsFlutterBinding.ensureInitialized();
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    String str = prefs.getString('loginInfo');
    String orderStr = prefs.getString('orderInfo');
    if (str != null) {
      info = LoginInfo.fromJSON(JsonDecoder().convert(str));
    }
    if (orderStr != null) {
      order = OrderInfo.fromJson(JsonDecoder().convert(orderStr));
    }
  }

  static setLoginInfo(LoginInfo newinfo) async {
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    info =  newinfo;
    
    bool bl = await prefs.setString('loginInfo', JsonEncoder().convert(newinfo.getJson()));
    if (bl) {
      print('数据更新成功');
    }
  }

  static setOrderInfo(OrderInfo newinfo) async {
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    order =  newinfo;
    
    bool bl = await prefs.setString('orderInfo', JsonEncoder().convert(newinfo.getJson()));
    if (bl) {
      print('数据更新成功');
    }
  }


  static removeLoginInfo() async {
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    prefs.remove('loginInfo');
    info = LoginInfo();
  }

  static removeOrderInfo() async {
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    final SharedPreferences prefs = await _prefs;
    prefs.remove('orderInfo');
    info = null;
  }

}

class FFChangeNotifier extends ChangeNotifier {
  LoginInfo info = FFShareInstance.info;

  set updateLoginInfo(LoginInfo tempInfo) {
    FFShareInstance.setLoginInfo(tempInfo);
    notifyListeners();
  }

  set updateOrderInfo(OrderInfo tempInfo) {
    FFShareInstance.setOrderInfo(tempInfo);
    notifyListeners();
  }

  removeOrderInfo() {
    FFShareInstance.removeOrderInfo();
    notifyListeners();
  }

  removeLoginInfo() {
    FFShareInstance.removeLoginInfo();
    notifyListeners();
  }
  
}

 Map<String, dynamic> menuList() {
 return {
    '人气Top':[
      ['榛果拿铁', '24']
    ],
    '大师咖啡':[
      ['拿铁', '24'],
      ['香草拿铁', '24'],
      ['焦糖拿铁', '27'],
      ['牛奶拿铁', '24'],
      ['瑞纳冰', '27'],
      ['啤酒拿铁', '24'],
    ],
    '健康轻食':[
      ['沙拉', '20'],
      ['草莓沙拉', '24'],
      ['苹果拿铁', '27'],
      ['香蕉拿铁', '24'],
      ['果冻冰', '27'],
      ['芒果拿铁', '24'],
    ],
  };
  }

  Map<String, dynamic> productStyle() {
    return {
      '规格':['大'],
      '温度':['热', '常温', '加冰'],
      '糖':['全糖', '7分糖', '半塘', '3分糖', '污唐'],
      '奶':['双份奶', '单份奶', '无奶'],
    };
  }